//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartAreaHistoricalView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartAreaHistoricalView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ID_TIMER			1
#define MAX_VAL				100.
#define GRID_COLUMN_WIDTH	40

/////////////////////////////////////////////////////////////////////////////
// CChartAreaHistoricalView

IMPLEMENT_DYNCREATE(CChartAreaHistoricalView, CBCGPChartExampleView)

CChartAreaHistoricalView::CChartAreaHistoricalView()
	: CBCGPChartExampleView(CChartAreaHistoricalView::IDD),
	m_brFill1(CBCGPColor::Pink, .3)
{
	//{{AFX_DATA_INIT(CChartAreaHistoricalView)
	m_nUpdateFr = 50;
	m_nValuesPerInterval = 10;
	m_bColorZone = TRUE;
	//}}AFX_DATA_INIT

	m_dblLastVal = 0;
	m_bIsRunning = FALSE;
	m_pRange = NULL;
}

CChartAreaHistoricalView::~CChartAreaHistoricalView()
{
}

void CChartAreaHistoricalView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartAreaHistoricalView)
	DDX_Control(pDX, IDC_VALUES_PER_INTERVAL, m_wndValuesPerInterval);
	DDX_Control(pDX, IDC_UPDATE_FR, m_wndUpdateFr);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Control(pDX, IDC_STOP, m_wndStop);
	DDX_Control(pDX, IDC_START, m_wndStart);
	DDX_Slider(pDX, IDC_UPDATE_FR, m_nUpdateFr);
	DDX_Slider(pDX, IDC_VALUES_PER_INTERVAL, m_nValuesPerInterval);
	DDX_Check(pDX, IDC_SHOW_ZONE, m_bColorZone);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartAreaHistoricalView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartAreaHistoricalView)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_SHOW_ZONE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartAreaHistoricalView diagnostics

#ifdef _DEBUG
void CChartAreaHistoricalView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartAreaHistoricalView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartAreaHistoricalView message handlers

void CChartAreaHistoricalView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	m_wndStop.EnableWindow(FALSE);

	m_wndUpdateFr.SetRange(15, 100, TRUE);
	m_wndValuesPerInterval.SetRange(GRID_COLUMN_WIDTH / 10, GRID_COLUMN_WIDTH, TRUE);

	UpdateData(FALSE);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartArea);
	pChart->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);
	pChart->SetThemeOpacity(60);

	CBCGPChartSeries* pSeries = pChart->GetSeries(0);
	ASSERT_VALID(pSeries);

	pSeries->EnableHistoryMode(TRUE, 1000);

	CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
	ASSERT_VALID(pXAxis);

	pXAxis->m_axisLabelType = CBCGPChartAxis::ALT_NO_LABELS;
	pXAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_NO_TICKS;
	pXAxis->SetFixedIntervalWidth(GRID_COLUMN_WIDTH, m_nValuesPerInterval);

	CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
	ASSERT_VALID(pYAxis);

	pYAxis->SetFixedDisplayRange(0., MAX_VAL, 20.);

	pChart->AddChartData(m_dblLastVal);

	m_pRange = pChart->AddChartRangeObject(50., 100., TRUE /* Horizontal */, m_brFill1);

	OnUpdateChart();
}

void CChartAreaHistoricalView::OnTimer(UINT_PTR nIDEvent)
{
	CBCGPChartExampleView::OnTimer(nIDEvent);

	if (nIDEvent != ID_TIMER || !IsWindowVisible())
	{
		return;
	}

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	const double d = MAX_VAL / 25;
	const double dblDelta = Rand(-d, d);

	const double dblVal = min(MAX_VAL - 1, max(0., m_dblLastVal + dblDelta));

	m_dblLastVal = dblVal;
	pChart->AddChartData(dblVal);

	pChart->SetDirty();
	pChart->Redraw();
}

void CChartAreaHistoricalView::OnStart() 
{
	UpdateData();

	SetTimer(ID_TIMER, m_nUpdateFr, NULL);

	m_wndStart.EnableWindow(FALSE);
	m_wndStop.EnableWindow();

	m_bIsRunning = TRUE;
}

void CChartAreaHistoricalView::OnStop() 
{
	KillTimer(ID_TIMER);

	m_wndStart.EnableWindow();
	m_wndStop.EnableWindow(FALSE);

	m_bIsRunning = FALSE;
}

void CChartAreaHistoricalView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	UpdateData();

	if (pScrollBar->GetSafeHwnd() == m_wndUpdateFr.GetSafeHwnd() && m_bIsRunning)
	{
		KillTimer(ID_TIMER);
		SetTimer(ID_TIMER, m_nUpdateFr, NULL);

		return;
	}

	if (pScrollBar->GetSafeHwnd() == m_wndValuesPerInterval.GetSafeHwnd())
	{
		CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
		ASSERT_VALID(pChart);

		CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
		ASSERT_VALID(pXAxis);

		pXAxis->SetFixedIntervalWidth(GRID_COLUMN_WIDTH, m_nValuesPerInterval);

		pChart->SetDirty();
		pChart->Redraw();

		return;
	}
}

void CChartAreaHistoricalView::OnUpdateChart() 
{
	UpdateData();

	m_pRange->SetVisible(m_bColorZone);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->Redraw();
}
